package com.www.applyword.ui.home;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.adapter.HotJobAdapter;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.bean.HotJobBean;
import com.www.applyword.bean.OrderBean;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.utils.SPUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * author : huang
 * date   : 2019/10/21 0021 14:44
 * version: 1.0
 *
 * @author Administrator
 */
public class HotJobActivity extends BaseActivity {
    @BindView(R.id.rl_hot_job)
    RecyclerView rlHotJob;


    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;

    private HotJobAdapter mHotJobAdapter;
    private List<HotJobBean> hotJobBeans = new ArrayList<>();
    private List<OrderBean> orderBeanList = new ArrayList<>();

    private int page = 1;
    private int Rows = 10;
    private int type;
    private String keyword;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.hot_job_activity);
        int type = getIntent().getIntExtra("type", 0);

        rlHotJob.setLayoutManager(new LinearLayoutManager(this));
        rlHotJob.setItemAnimator(new DefaultItemAnimator());
        mHotJobAdapter = new HotJobAdapter(this);
        rlHotJob.setAdapter(mHotJobAdapter);

        keyword = getIntent().getStringExtra("Keyword");
        if (!TextUtils.isEmpty(keyword)){
            setTitle("搜索结果");
            Map<String, Object> params = new HashMap<>();
            params.put("Page","1");
            params.put("Rows","20");
            params.put("Keyword", keyword);
            HttpClient.get(HotJobActivity.this, Constants.INDEXHOTSEARCHORDERLIST, params,new CallBack<List<OrderBean>>() {
                @Override
                public void onSuccess(List<OrderBean> orderBeans) {
                    showEmpty(true,vsEmpty,0);
                    if (orderBeans.size() == 0){
                      //  showEmpty(true,vsEmpty,0);
                    }else {
                      //  showEmpty(false, vsEmpty,0);
                    }
                  //  mHotJobAdapter.replaceAll(orderBeans, true);
                }
            });
            ib_back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("respond", "Hello,Alice!I'm Bob.");
                    // 设置返回码和返回携带的数据
                    setResult(Activity.RESULT_OK, intent);
                    // RESULT_OK就是一个默认值，=-1，它说OK就OK吧
                    finish();
                }
            });

        }else {
            if (type == 1) {
                setTitle("热门职位");
            } else if (type == 2) {
                setTitle("最新职位");
            } else if (type == 3) {
                setTitle("离我最近");
            } else if (type == 4) {
                setTitle("名企直招");
            }
        }

        mHotJobAdapter.setOnItemClickListener((viewHolder, view, i) ->
                startActivity(new Intent(HotJobActivity.this, HotJobDetailsActivity.class)
                        .putExtra("OrderID", mHotJobAdapter.getItem(i).getOrder_ID())));
    }

    @Override
    protected void loadData() {
        if (!TextUtils.isEmpty(keyword)){
            return;
        }
        type = getIntent().getIntExtra("type", 0);
        refresh.setOnRefreshListener(refreshLayout -> getOrderList(true));

        refresh.setOnLoadMoreListener(refreshLayout ->getOrderList(false));

        getOrderList(true);
    }

    //99 代表全部
    private void getOrderList(boolean isRefresh) {
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("Page", page + "");
        params.put("Rows", Rows + "");
        if (type == 1) {
            params.put("PositionType", 1 + "");
            params.put("AddressCity", "");
            params.put("FamousEnterprisesType", 99 + "");
            params.put("SalaryType", 99 + "");
        } else if (type == 2) {
            params.put("PositionType", 99 + "");
            params.put("AddressCity", "");
            params.put("FamousEnterprisesType", 99 + "");
            params.put("SalaryType", 99 + "");
        } else if (type == 3) {
            params.put("PositionType", 99 + "");
            params.put("AddressCity", SPUtil.getString("city"));
            params.put("FamousEnterprisesType", 99 + "");
            params.put("SalaryType", 99 + "");
        } else {
            params.put("PositionType", 99 + "");
            params.put("AddressCity", "");
            params.put("FamousEnterprisesType", 1 + "");
            params.put("SalaryType", 99 + "");
        }

        params.put("PositionID", "");
        params.put("Sex", "");
        params.put("Welfare", "");

        HttpClient.get(HotJobActivity.this, Constants.GETORDERLIST, params, new CallBack<List<OrderBean>>() {
            @Override
            public void onSuccess(List<OrderBean> orderBeans) {
                if (isRefresh){
                refresh.finishRefresh(true);
                if (orderBeans.size() == 0){
                    showEmpty(true,vsEmpty,0);
                }else {
                    showEmpty(false, vsEmpty,0);
                }
                mHotJobAdapter.replaceAll(orderBeans, true);
            }else {
                if (orderBeans.size() > 0) {
                    refresh.finishLoadMore(true);
                    mHotJobAdapter.addAll(orderBeans);
                }else {
                    refresh.finishLoadMoreWithNoMoreData();
                }
            }
            page++;
         }
        });

    }
}
